//
//  FriendBasicCell.m
//  SportSocial
//
//  Created by yull on 15/1/3.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "FriendBasicCell.h"

@implementation FriendBasicCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        CGRect rect = CGRectMake(10, 0, 50, 25);
        UILabel *addressTitleLabel = [[UILabel alloc] initWithFrame:rect];
        addressTitleLabel.text = @"所在地";
        addressTitleLabel.font = [UIFont systemFontOfSize:16.0f];
        addressTitleLabel.textAlignment = NSTextAlignmentCenter;
        addressTitleLabel.textColor = [UIColor grayColor];
        addressTitleLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:addressTitleLabel];
        
        rect.origin.y += rect.size.height;
        UILabel *sexLabel = [[UILabel alloc] initWithFrame:rect];
        sexLabel.text = @"性  别";
        sexLabel.font = [UIFont systemFontOfSize:16.0f];
        sexLabel.textAlignment = NSTextAlignmentCenter;
        sexLabel.textColor = [UIColor grayColor];
        sexLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:sexLabel];
        
        rect.origin.y += rect.size.height;
        UILabel *signTitleLabel = [[UILabel alloc] initWithFrame:rect];
        signTitleLabel.text = @"签  名";
        signTitleLabel.font = [UIFont systemFontOfSize:16.0f];
        signTitleLabel.textAlignment = NSTextAlignmentCenter;
        signTitleLabel.textColor = [UIColor grayColor];
        signTitleLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:signTitleLabel];
        
        CGFloat orginX = addressTitleLabel.right + addressTitleLabel.left;
        rect = addressTitleLabel.frame;
        rect.origin.x = orginX;
        rect.size.width = self.contentView.width - orginX - addressTitleLabel.left;
        self.addressLabel = [[UILabel alloc] initWithFrame:rect];
        self.addressLabel.font = [UIFont systemFontOfSize:16.0f];
        self.addressLabel.textColor = [UIColor blackColor];
        self.addressLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.addressLabel];
        
        rect.origin.y = signTitleLabel.top;
        self.signLabel = [[UILabel alloc] initWithFrame:rect];
        self.signLabel.font = [UIFont systemFontOfSize:16.0f];
        self.signLabel.textColor = [UIColor blackColor];
        self.signLabel.backgroundColor = [UIColor clearColor];
        self.signLabel.numberOfLines = 0;
        [self.contentView addSubview:self.signLabel];
        
        rect = sexLabel.frame;
        rect.origin.x = orginX;
        rect.size.width = sexLabel.height;
        self.sexImageView = [[UIImageView alloc] initWithFrame:rect];
        [self.contentView addSubview:self.sexImageView];
    }
    return self;
}

+ (CGFloat)calculateCellHeightWithUser:(UserModel *)userModel
{
    return 50.0f + [self getSignLabelHeightWithText:userModel.signature];
}

- (void)drawCellWithUserModel:(UserModel *)userModel
{
    self.addressLabel.text = userModel.address;
    
    NSString *imageName = userModel.sex == 1 ? @"female_icon" : @"male_icon";
    self.sexImageView.image = [UIImage imageNamed:imageName];
    
    self.signLabel.text = userModel.signature;
    self.signLabel.height = [FriendBasicCell getSignLabelHeightWithText:userModel.signature];
}

+ (CGFloat)getSignLabelHeightWithText:(NSString *)text
{
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH - 60, 25);
    UILabel *heightLabel = [[UILabel alloc] initWithFrame:rect];
    heightLabel.font = [UIFont systemFontOfSize:16.0f];
    heightLabel.numberOfLines = 0;
    heightLabel.text = text;
    
    CGFloat height = [heightLabel sizeThatFits:CGSizeMake(rect.size.width, CGFLOAT_MAX)].height;
    return MAX(height, rect.size.height);
}
@end
